function make_holdout(session,holdout_list,flag)
%MAKE_HOLDOUT executes step three of the work flow, creating 10
%holdout samples with 10% of available votes.  The new roll call matrices
%are exported to csv files to facilitate running W-NOMINATE in R.
%
%SESSION is the senate to be processed
%HOLDOUT_LIST is the list of holdouts to create
%FLAG determines house (1) or senate (0)

%Load the data

    if flag==0
        load(['./data/senate-',num2str(session),'.mat']);
    else
        load(['./data/house-',num2str(session),'.mat']);
    end

%Make a copy of the voting matrix and change the 0 and 9 entries to NaN
%We do this so that we can sample only votes where people voted yes or no
vts=votesm;
vts(votesm==9)=NaN;
vts(votesm==0)=NaN;
try
    nms=name;
    nms(missed)=[];
catch
    for i=1:size(vts,1)
        nms{i}=num2str(i);
    end
end
%Get rid of any quotes and commas in the strings
for i=1:numel(nms)
    nms{i}(nms{i}=='"')=[];
    nms{i}(nms{i}==',')=[];
end

%Find the total number of votes
numvotes=numel(find(isnan(vts)==0));
%Calculate the number of votes to hold out
numholdout=round(numvotes*0.1);

%Create different samples and save them
for j=holdout_list
    clear hinds votesh
    %Randomly select the sample
    hinds=randsample(find(isnan(vts)==0),numholdout);
    %Set these votes as missing
    votesh=vts;
    votesh(hinds)=0;
    %Change the rest of the NaN back to missing data or abstentions
    votesh(isnan(votesh))=0;
    votesh(votesm==9)=9;
    %Find any lopsided votes or disqualified legislators that may have been
    %created with the held out data
        [votesmh,missed,lopsided_votes]=clean_data(votesh);
    %Throw away any of the holdout indices that are removed
    tmpmat=zeros(size(votesh));
    tmpmat(hinds)=1;
    tmpmat(missed,:)=[];
    tmpmat(:,lopsided_votes)=[];
    hinds=find(tmpmat==1);
    
    %Record the "ground truth" - the original votes
    votes_no_holdout=votesm;
    votes_no_holdout(missed,:)=[];
    votes_no_holdout(:,lopsided_votes)=[];
    %Save these to a MATLAB file
   
        if flag==0
            save(['./holdout samples/senate-',num2str(session),'-holdout-',num2str(j),'.mat'],...
                'votesmh','votes_no_holdout','missed','lopsided_votes','votesh','hinds');
        else
            save(['./holdout samples/house-',num2str(session),'-holdout-',num2str(j),'.mat'],...
                'votesmh','votes_no_holdout','missed','lopsided_votes','votesh','hinds');
        end
    
    %Now, write these to a csv file.
    nmem=size(votesmh,1);
    nvts=size(votesmh,2);
    %convert to standard WNOMINATE scoring.
    csvvotes=zeros(size(votesmh));
    csvvotes(votesmh==1)=1;
    csvvotes(votesmh==-1)=6;
    csvvotes(votesmh==0)=9;
    csvvotes(isnan(votesmh))=9;
    
        if flag==0
            fid=fopen(['./holdout samples/senate-',num2str(session),'-holdout-',num2str(j),'.csv'],'w');
        else
            fid=fopen(['./holdout samples/house-',num2str(session),'-holdout-',num2str(j),'.csv'],'w');
        end
        
        for k1=1:nmem
            fprintf(fid,'%s,',nms{k1});
            for k2=1:nvts-1
                fprintf(fid,'%u,',csvvotes(k1,k2));
            end
            fprintf(fid,'%u\n',csvvotes(k1,end));
        end
        fclose(fid);
    
end
end
